﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Business;
using Modelo;

namespace GestorUnidesc.Cadastro.Login
{
    public partial class Etapa1 : System.Web.UI.UserControl
    {
        UsuarioBO objUsuBO = new UsuarioBO();
        UsuarioVO objUsuVO = new UsuarioVO();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnInserir_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["idUsuario"] != null && !string.IsNullOrEmpty(Session["idUsuario"].ToString()))
                {
                    objUsuVO.idUsuario = Convert.ToInt32(Session["idUsuario"].ToString());
                }

                if (txtSenhaUsuario.Text.Count() < 6 || txtSenhaUsuarioConfirma.Text.Count() < 6)
                {
                    ScriptManager.RegisterStartupScript(btnInserir, btnInserir.GetType(), "alert", "alert('Sua senha Precisa ter no mínimo 6 caracteres');", true);
                }
                else if (FormsAuthentication.HashPasswordForStoringInConfigFile(txtSenhaUsuario.Text, "md5") == FormsAuthentication.HashPasswordForStoringInConfigFile(txtSenhaUsuarioConfirma.Text, "md5"))
                {
                    objUsuVO.idSetor = Convert.ToInt32(ddlSetor.SelectedValue);
                    objUsuVO.IdNivelUsuario = Convert.ToInt32(ddlTipoUsuario.SelectedValue);
                    objUsuVO.cpfUsuario = txtCpfUsuario.Text.Replace(".", "");
                    objUsuVO.nomeUsuario = txtNomeUsuario.Text;
                    objUsuVO.loginUsuario = txtLoginUsuario.Text.Replace(" ", "");
                    objUsuVO.senhaUsuario = FormsAuthentication.HashPasswordForStoringInConfigFile(txtSenhaUsuario.Text, "md5");
                    objUsuVO.Status = Convert.ToBoolean(rblStatusUsuario.SelectedValue);
                    objUsuVO.primeiroAcesso = Convert.ToBoolean(ckbPrimeiroAcesso.Checked);
                    objUsuBO.InserirUsuario(objUsuVO);
                    limparTela();
                    GridView1.DataBind();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(btnInserir, btnInserir.GetType(), "alert", "alert('Senhas não conferem!');", true);
                }


            }
            catch (Exception)
            {

                ScriptManager.RegisterStartupScript(btnInserir, btnInserir.GetType(), "alert", "alert('Erro ao Inserir Usuário!');", true);
            }
        }
        public void limparTela()
        {
            txtNomeUsuario.Text = "";
            txtCpfUsuario.Text = "";
            txtLoginUsuario.Text = "";
            txtSenhaUsuario.Text = "";
            txtSenhaUsuarioConfirma.Text = "";
            ddlSetor.SelectedIndex = 0;
            rblStatusUsuario.SelectedValue = "True";
            Session["idUsuario"] = null;

        }
        public string CpfFormat(object cpf)
        {

            try
            {

                Int64 iCpf = Convert.ToInt64(cpf);

                return string.Format(@"{0:###\.###\.###-##}", iCpf);

            }

            catch (Exception ex)
            {

                throw ex;

            }

        }
        protected void ddlSetor_DataBound(object sender, EventArgs e)
        {
            ddlSetor.Items.Insert(0, "Selecione...");
        }
        protected void btnDesativar_Click(object sender, EventArgs e)
        {
            int idUsuario = Convert.ToInt32(((Button)sender).CommandArgument.ToString());
            objUsuBO.alterarStatusUsuario(idUsuario);
            GridView1.DataBind();

        }
        protected void btnEditar_Click(object sender, EventArgs e)
        {
            int idUsuario = Convert.ToInt32(((Button)sender).CommandArgument.ToString());
            objUsuVO = objUsuBO.SelectUsuario(idUsuario);
            txtNomeUsuario.Text = objUsuVO.nomeUsuario;
            txtCpfUsuario.Text = objUsuVO.cpfUsuario;
            txtLoginUsuario.Text = objUsuVO.loginUsuario;
            rblStatusUsuario.SelectedValue = objUsuVO.Status.ToString();
            ddlSetor.SelectedValue = objUsuVO.idSetor.ToString();
            ckbPrimeiroAcesso.Checked = objUsuVO.primeiroAcesso;
            Session["idUsuario"] = objUsuVO.idUsuario;
        }

        protected void btnLimpar_Click(object sender, EventArgs e)
        {
            limparTela();
        }
    }
}